package com.offcn.user.controller;

import com.offcn.entity.Result;
import com.offcn.entity.StatusCode;
import com.offcn.user.pojo.Address;
import com.offcn.user.service.AddressService;
import com.offcn.utils.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping("/list")
    public Result<List<Address>> findAddressListByUsername(){
        // 获取令牌数据
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String token = details.getTokenValue();
        // 通过令牌解析出用户名
        String username = TokenDecode.getUsername(token);
        List<Address> addressList = addressService.findAddressListByUsername(username);
        // 返回结果
        return new Result<>(true, StatusCode.OK,addressList,"查询地址成功");
    }
    //新增用户地址添加
    @PostMapping("/add")
    public Result addAddress(@RequestBody Address address){
        try {
            // 获取令牌数据
            OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
            String token = details.getTokenValue();
            // 通过令牌解析出用户名
            String username = TokenDecode.getUsername(token);

            address.setUserId(username);
            addressService.saveAddress(address);
            // 返回结果
            return new Result<>(true, StatusCode.OK,"用户地址新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR,"用户地址新增失败");
        }

    }

    //用户地址修改
    @PutMapping("/update")
    public Result update(@RequestBody Address address){
        try {
            // 获取令牌数据
            OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
            String token = details.getTokenValue();
            // 通过令牌解析出用户名
            String username = TokenDecode.getUsername(token);


            address.setUserId(username);
            addressService.updateAddress(address);
            // 返回结果
            return new Result<>(true, StatusCode.OK,"用户地址修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR,"用户地址修改失败");
        }
    }

    //新增接口，设置地址为默认地址，把该用户的其他地址设置为非默认
    @PutMapping("/isdefault")
    public Result isDefault(@RequestBody Address address){
        //判断address参数是否有传递id
        if(address==null||address.getId()==null){
            return new Result<>(false,StatusCode.ERROR,"修改用户默认地址出错");
        }
        try {
            // 获取令牌数据
            OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
            String token = details.getTokenValue();
            // 通过令牌解析出用户名
            String username = TokenDecode.getUsername(token);

            //根据用户名获取对应的全部用户数据
            List<Address> addressList = addressService.findAddressListByUsername(username);
            for (Address addressOld : addressList) {
                //修改默认地址为0
                addressOld.setIsDefault("0");
                //更新保存回数据库
                addressService.updateAddress(addressOld);
            }

            //最后修改当前地址为默认地址
            address.setIsDefault("1");
            addressService.updateAddress(address);

            return new Result<>(true,StatusCode.OK,"修改默认地址成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false,StatusCode.ERROR,"修改用户默认地址出错");
        }


    }
}
